<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

  <style>
    ul {
      display: flex;
    }

    ul li {
      list-style: none;
      width: 30px;
      height: 30px;
      margin: 5px;
      text-align: center;
    }

    .active {
      background: blue;
      color: white;
    }

    .mask {
      position: absolute;
      width: 100%;
      height: 100%;
      background: rgba(0, 0, 0, 0.2);
      z-index: 1;
      top: 0;
      left: 0;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .dialog {
      width: 300px;
      height: 200px;
      background: white;
    }
  </style>
</head>

<body>
  <div id="app">
    <input type="text" v-model="txt">
    <button @click="handleSearch">查询</button>
    <button @click="showAdd = true">添加</button>
    <table>
      <tr>
        <td>公司名称</td>
        <td>职位名称</td>
        <td>
          薪水
        </td>
        <td>
          操作
        </td>
      </tr>
      <tr v-for="item in getDatalist">
        <td>
          {{item.companyName}}
        </td>
        <td>
          {{item.positionName}}
        </td>
        <td>
          {{item.salary}}
        </td>
        <td>
          <button @click="handleEdit(item)">编辑</button>
          <button @click="handleDel(item._id)">删除</button>
        </td>
      </tr>
    </table>

    <ul>
      <li @click="changePage(n)" :class="{active: pageno===n }" v-for="n in getPages">{{n}}</li>
    </ul>

    <custom-dialog v-if="showAdd">
      <template #title>
        添加
      </template>

      <template #body>
        职位名称: <input type="text" v-model="current.positionName" name="" id="">
        </br>
        <button @click="handleAdd">添加</button>
        <button @click="showAdd = false">取消</button>
      </template>



    </custom-dialog>

    <custom-dialog v-if="showEdit">

      <template #title>
        编辑
      </template>

      <template #body>
        职位名称: <input type="text" v-model="editItem.positionName" name="" id="">
        </br>
        <button @click="handleEditSubmit">更新</button>
        <button @click="showEdit = false">取消</button>
      </template>

    </custom-dialog>

  </div>

  <script type="text/template" id="temp">
    <div class="mask">
        <div class="dialog">
          <h1>
          <slot name="title"></slot>
          </h1>
          <hr>
          <slot name="body"></slot>
        </div>
    </div>
  </script>
  <script>


    Vue.component('custom-dialog', {
      template: `#temp`
    })


    var vm = new Vue({
      el: "#app",
      data: {
        //每页展示数据条数
        pagesize: 5,
        // 页码
        pageno: 1,
        datalist: [],
        txt: '',
        //搜索关键字
        keyword: '',
        //是否显示添加的对话框
        showAdd: false,
        //是否显示编辑的对话框
        showEdit: false,
        current: {
          positionName: ''
        },
        editItem: {
          _id: '',
          positionName: ''
        }
      },
      methods: {
        changePage(n) {
          this.pageno = n;
        },
        handleSearch() {
          this.pageno = 1;
          this.keyword = this.txt;
        },
        handleDel(id) {
          this.datalist.find((item, index) => {
            if (item._id === id) {
              this.datalist.splice(index, 1)
              return true;
            }
          })
        },
        handleAdd() {
          this.datalist.push({
            _id: new Date().getTime().toString(16),
            positionName: this.current.positionName
          })
        },

        handleEdit(item) {
          this.showEdit = true;
          this.editItem = Object.assign({}, item);
        },
        handleEditSubmit() {
          this.datalist.find((item, index) => {
            if (item._id === this.editItem._id) {
              let tmp = { ...item }
              tmp.positionName = this.editItem.positionName;
              this.datalist.splice(index, 1, tmp)
              this.showEdit = false;
              return true;
            }
          })
        }
      },
      computed: {
        getPages() {
          let tmp = this.datalist.filter((item) => {
            return item.positionName.indexOf(this.keyword) > -1
          })
          return Math.ceil(tmp.length / this.pagesize);
        },
        getDatalist() {

          let tmp = this.datalist.filter((item) => {
            return item.positionName.indexOf(this.keyword) > -1
          })

          let start = (this.pageno - 1) * this.pagesize;
          let end = start + this.pagesize;
          return tmp.slice(start, end)
        }
      },
      created() {
        fetch('./data.json').then((res) => res.json()).then((res) => {
          console.log(res)
          this.datalist = res.data;
        })
      }
    })

  </script>

</body>

</html>